# -*- coding: utf-8 -*-

from django.db import models
from django.db.models.base import Model
from django.db.models.fields.files import FileField, ImageField
from django.db.models.fields import CharField, TextField, DateField, AutoField
from mainapp.helpers import parse_attrs, parse_options, parse_range
from ast import literal_eval
from django.db.models import permalink


class ClassifferLearningData(Model):
    
    id = AutoField(primary_key=True)
    
    file = FileField(upload_to='datasets/learning/', verbose_name=u'Zbiór treningowy', blank=True)
    
    module = CharField(max_length=50, verbose_name=u'Moduł', blank=True)
    attrs = CharField(max_length=200, verbose_name=u'Atrybuty', blank=True)
    options = CharField(max_length=200, verbose_name=u'Opcje', blank=True)
    
    rows = CharField(max_length=10, verbose_name=u'Wiersze', blank=True)
    data_columns = CharField(max_length=10, verbose_name=u'Kolumny z danymi', blank=True)
    classes_column = CharField(max_length=10, verbose_name=u'Kolumna z klasami', blank=True)
    delimiter = CharField(max_length=2, verbose_name=u'Separator', blank=True)
    
    created_at = DateField(auto_now_add=True)

    def get_parsed(self):
        u'''Zwraca przeparsowany objekt w postaci słownika'''
        
        
        attrs = parse_attrs([ item.strip() for item in self.attrs.split(';')])
        options = parse_options([ item.strip() for item in self.options.split(';')])
        
        file = self.file
        rows = self.rows
        data_columns = parse_range(self.data_columns)
        classes_column = parse_range(self.classes_column)
        delimiter = self.delimiter
        
        return locals()

    @permalink
    def get_absolute_url(self):
        return ('supervised_load', (), {'id':self.id,})
    
    class Meta:
        
        ordering = ['-created_at']

class UnsupervisedData(Model):
    
    id = AutoField(primary_key=True)
    
    file = FileField(upload_to='datasets/unsupervised/', verbose_name=u'Zbiór treningowy', blank=True)
    
    module = CharField(max_length=50, verbose_name=u'Moduł', blank=True)
    attrs = CharField(max_length=200, verbose_name=u'Atrybuty', blank=True)
    options = CharField(max_length=200, verbose_name=u'Opcje', blank=True)
    
    rows = CharField(max_length=10, verbose_name=u'Wiersze', blank=True)
    data_columns = CharField(max_length=10, verbose_name=u'Kolumny z danymi', blank=True)
#    classes_column = CharField(max_length=10, verbose_name=u'Kolumna z klasami', blank=True)
    delimiter = CharField(max_length=2, verbose_name=u'Separator', blank=True)
    
    created_at = DateField(auto_now_add=True)

    def get_parsed(self):
        u'''Zwraca przeparsowany objekt w postaci słownika'''
        
        
        attrs = parse_attrs([ item.strip() for item in self.attrs.split(';')])
        options = parse_options([ item.strip() for item in self.options.split(';')])
        
        file = self.file
        rows = self.rows
        data_columns = parse_range(self.data_columns)
#        classes_column = literal_eval(self.classes_column)
        delimiter = self.delimiter
        
        return locals()

    @permalink
    def get_absolute_url(self):
        return ('unsupervised_load', (), {'id':self.id,})
    
    
    class Meta:
        
        ordering = ['-created_at']

class Result(Model):
    
    image = ImageField(upload_to='images/plots', default='')
    description = TextField(default='')
    
    created_at = DateField(auto_now_add=True)
    updated_at = DateField(auto_now = True)
    
    @permalink
    def get_absolute_url(self):
        return ('dm_result', (self.id,), {})
    
    

#    
#class ClasssifferTestingData(Model):
#    
#    file = FileField(upload_to='datasets/tests/')
#    rows = CharField(max_length=10)
#    columns = CharField(max_length=10)
#    delimiter = CharField(max_length=2)
    

    
    